package com.techwells.learningkites.adapter;

import java.io.IOException;
import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.techwells.learningkites.R;
import com.techwells.learningkites.networkaccess.KitesRequestUrl;
import com.techwells.learningkites.networkservice.model.Favorite;
import com.techwells.learningkites.networkservice.model.Sentences;
import com.techwells.learningkites.networkservice.model.Word;
import com.techwells.taco.widget.OnDeleteListioner;


public class MySentenceListAdapter extends BaseAdapter {

	private Context mContext;
	private List<Sentences> mlist = null;
	private OnDeleteListioner mOnDeleteListioner;
	private boolean delete = false;
	private MediaPlayer mp;

	// private Button curDel_btn = null;
	// private UpdateDate mUpdateDate = null;

	public MySentenceListAdapter(Context mContext, List<Sentences> mlist) {
		this.mContext = mContext;
		this.mlist = mlist;
		mp = new MediaPlayer();
	}

	public void setDelete(boolean delete) {
		this.delete = delete;
	}

	public boolean isDelete() {
		return delete;
	}

	public void setOnDeleteListioner(OnDeleteListioner mOnDeleteListioner) {
		this.mOnDeleteListioner = mOnDeleteListioner;
	}

	public int getCount() {

		return mlist.size();
	}

	public Object getItem(int pos) {
		return mlist.get(pos);
	}

	public long getItemId(int pos) {
		return pos;
	}

	public View getView(final int pos, View convertView, ViewGroup p) {

		final ViewHolder viewHolder;
		if (convertView == null) {
			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.item_mysentencelist, null);
			viewHolder = new ViewHolder();
			viewHolder.num = (TextView) convertView.findViewById(R.id.num);
			viewHolder.enLang = (TextView) convertView.findViewById(R.id.en_lang);
			viewHolder.cnLang = (TextView) convertView.findViewById(R.id.cn_lang);
			viewHolder.sound = (ImageView) convertView.findViewById(R.id.sound);
			viewHolder.delete_action = (TextView) convertView.findViewById(R.id.delete_action);
			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		
		if (mlist.get(pos).sequence < 10) {
			viewHolder.num.setText(mlist.get(pos).sequence + "." + "\t");
		}else {
			viewHolder.num.setText(""+mlist.get(pos).sequence + ".");
		}
        
        String enString = mlist.get(pos).enLang.replaceAll("\r|\n", "");
        String cnString = mlist.get(pos).cnLang.replaceAll("\r|\n", "");
        viewHolder.enLang.setText(enString);
        viewHolder.cnLang.setText(cnString);
        
        viewHolder.sound.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				try {
					if (mp.isPlaying()) {
						mp.stop();
					}
					mp.reset();
					mp.setDataSource(KitesRequestUrl.BASE_URL_IMG + mlist.get(pos).resource);
					mp.prepare();
					mp.setOnPreparedListener(new OnPreparedListener() {
						
						@Override
						public void onPrepared(MediaPlayer mp) {
							mp.start();
							
						}
					});
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (SecurityException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalStateException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		});

		final OnClickListener mOnClickListener = new OnClickListener() {

			@Override
			public void onClick(View view) {
				if (mOnDeleteListioner != null)
					mOnDeleteListioner.onDelete(pos);

			}
		};
		viewHolder.delete_action.setOnClickListener(mOnClickListener);
		return convertView;
	}

	public static class ViewHolder {
		TextView num,dialogue,enLang,cnLang;
    	ImageView sound;
		public TextView delete_action;

	}

}
